package controller;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.RoomService;
import pojo.Room;
import java.util.List;

@RestController
@RequestMapping("/rooms")
@Api(tags = "房间管理", description = "房间相关的CRUD操作")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @PostMapping("/add")
    @ApiOperation(value = "新增房间", notes = "添加一个新的房间信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Integer.class),
            @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public int addRoom(@ApiParam(value = "房间实体", required = true) @RequestBody Room room) {
        return roomService.insertRoom(room);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除房间", notes = "根据房间ID删除房间")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Integer.class),
            @ApiResponse(code = 404, message = "房间不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public int deleteRoom(@ApiParam(value = "房间ID", required = true) @PathVariable("id") int roomId) {
        return roomService.deleteRoomById(roomId);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新房间", notes = "更新房间的基本信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Integer.class),
            @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 404, message = "房间不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public int updateRoom(@ApiParam(value = "房间实体(包含ID)", required = true) @RequestBody Room room) {
        return roomService.updateRoom(room);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取房间详情", notes = "根据房间ID获取房间信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Room.class),
            @ApiResponse(code = 404, message = "房间不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public Room getRoom(@ApiParam(value = "房间ID", required = true) @PathVariable("id") int roomId) {
        return roomService.selectRoomById(roomId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取房间列表", notes = "获取所有房间的基本信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Room.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public List<Room> getAllRooms() {
        return roomService.selectAllRooms();
    }

    @GetMapping("/department/{departmentId}")
    @ApiOperation(value = "根据科室查询房间", notes = "根据科室ID获取该科室下的所有房间")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Room.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public List<Room> getRoomsByDepartment(@ApiParam(value = "科室ID", required = true) @PathVariable("departmentId") int departmentId) {
        return roomService.selectRoomsByDepartmentId(departmentId);
    }

    @GetMapping("/floor/{floor}")
    @ApiOperation(value = "根据楼层查询房间", notes = "根据楼层获取该楼层的所有房间")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Room.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public List<Room> getRoomsByFloor(@ApiParam(value = "楼层", required = true) @PathVariable("floor") int floor) {
        return roomService.selectRoomsByFloor(floor);
    }
    @GetMapping("/rooms/by-department/{departmentId}")
    @ResponseBody
    public List<Room> getRoomsByDepartmentId(@PathVariable Integer departmentId) {
        return roomService.getRoomsByDepartmentId(departmentId);
    }
} 